//显示菜单
showMenus();
//用户信息
showUserInfo();
//显示日期
setInterval("clock()",1000);
//弹窗弹出样式
var formShow = {
	"top": "25vh",
	"transition": "top .5s ease",
}
//弹窗收起样式
var formHide={ 
		"top": "-2000px", 
		"transition": "top .5s ease" 
}

function showUserInfo(){
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/userinfos',
		data:{
			"id":$("#myId").val()
		},
		success:function(data){
			var json=JSON.parse(data);
//			console.log(json);
			$(".info").eq(0).text('').text(json.userName);
			$(".info").eq(1).text('').text(json.roleName);
			$(".info").eq(2).text('').text(json.departName);
			$(".info").eq(3).text('').text(json.msgCount);
		},
		error:function(res){
			console.log(res);
		}
	});	
}

function showMenus(){
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/menu',
		data:{"id":$("#roleId").val()},
		success:function(data){
			var menus=JSON.parse(data);
//			console.log(menus);
			var ms="<div class='menu first' onMouseenter='showSub(this)' value='"+menus[0].nodeId+"'>"+menus[0].displayName+"</div>";
			
			for(var i=1;i<menus.length;i++){
				ms+="<div class='menu' onMouseenter='showSub(this)' value='"+menus[i].nodeId+"'>"+menus[i].displayName+"</div>";			
			}
			
			$(".menus").eq(0).empty().append(ms);
		},
		error:function(res){
			console.log(res);
		}
	});
}

//展示子菜单
function showSub(obj){
	var nodeId=$(obj).attr("value");
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/SubMenu',
		data:{
			"id":nodeId,
			"roleId":$("#roleId").val()
			},
		success:function(data){
//			console.log(data);
			var menus=JSON.parse(data);
			var ms="<div class='subMenus'><div class='subMenu first'><a href='"+menus[0].nodeUrl+"'>"+menus[0].displayName+"</a></div>";
			for(var i=1;i<menus.length;i++){
				ms+="<div class='subMenu'><a href='"+menus[i].nodeUrl+"'>"+menus[i].displayName+"</a></div>";
			}
			ms+="</div>";
			$(obj).append(ms);
		},
		error:function(res){
			console.log(res)
		}
	});
}

//日期显示
function clock()
{
	var d=new Date();
	var t=d.toLocaleString();
	$("#today").text(t);
}
Date.prototype.Format = function (fmt) { 
    var o = {
        "M+": this.getMonth() + 1, 
        "d+": this.getDate(), 
        "h+": this.getHours(), 
        "m+": this.getMinutes(), 
        "s+": this.getSeconds(), 
        "q+": Math.floor((this.getMonth() + 3) / 3), 
        "S": this.getMilliseconds() 
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
}


//隐藏弹窗
function coverBack(frm) {
	$("#cover").hide();
	$(frm).css(formHide);
} 

//获取部门列表(获取所有部门列表)
function getDepts(){
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/departinfo',
		data:{
			"typeName":"DepartInfo",
			"method":"getPageDatas"
		},
		success:function(data){
			var json=JSON.parse(data);
//			console.log(json);
			var ops="";
			for(var i=0;i<json.length;i++){
				ops+="<option value='"+json[i].departId+"'>"+json[i].departName+"</option>";
			}
//			console.log(ops);
			$("#depts").empty().append(ops);
//			console.log($("#depts").val());
			getBranches($("#depts").val());
		},
		error:function(res){
			console.log("失败");
		}
	});
}

//获取指定部门
function getGivenDept(opt){
	var branchId=$(opt).val();
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/departinfo',
		data:{
			"typeName":"departInfo",
			"method":"getDeparts",
			"branchId":branchId
		},
		success:function(data){
			var j=JSON.parse(data);
//			console.log(j);
			var ops="";
			for(var i=0;i<j.length;i++){
				ops+="<option value='"+j[i].departId+"'>"+j[i].departName+"</option>";	
			}
//			console.log(ops);
//			debugger;
			$("#depts").empty().append(ops);		
		},
		error:function(res){
			console.log("失败");
		}
	});
}

//获取机构列表(有关联部门表)
function getBranches(departId){
//	alert("加载机构下拉框");
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/branchdepart',
		data:{
			"departId":departId
		},
		success:function(data){
			var j=JSON.parse(data);
			var ops="<option value='"+j.branchId+"'>"+j.branchName+"</option>";		
//			console.log(ops);
//			debugger;
			$("#orgs").empty().append(ops);		
		},
		error:function(res){
			console.log("失败");
		}
	});
}

//获取机构列表(无关联部门列表)
getNormalBranches();
function getNormalBranches(){
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/branchinfo',
		data:{
			"typeName":"BranchInfo",
			"method":"getPageDatas"
		},
		success:function(data){
			var j=JSON.parse(data);
			var ops="<option value='0'>请选择</option>";
			for(var i=0;i<j.length;i++){
				ops+="<option value='"+j[i].branchId+"'>"+j[i].branchName+"</option>";		
			}
//			console.log(ops);
//			debugger;
			$("#orgs").empty().append(ops);
		},
		error:function(res){
			console.log("失败");
		}
	});
}


//显示提示
$("#addPrincipal").hover(function(){
	$(".addTip").eq(0).css('display','block');
},function(){
	$(".addTip").eq(0).css('display','none');
});

//获取负责人
$("#addPrincipal").click(function(){
	$(".principals").eq(0).toggleClass("showPrincipals");
	getPrincipal();
});

//加载负责人数据
function getPrincipal(){
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/Principals',
		success:function(data){
//			console.log(data);
			var branches=JSON.parse(data);
			var trees="<ul>";
			for(var i=0;i<branches.length;i++){	
				trees+="&nbsp;&nbsp;&nbsp;&nbsp;<li value='"+branches[i].branchId+"'>"+branches[i].branchName+"<ul>";
				if(branches[i].depts.length>0){
					var depts=branches[i].depts;
					for(var j=0;j<depts.length;j++){
						trees+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<li value='"+depts[j].departId+"'>"+depts[j].departName+"</li><ul>";
//						console.log(depts[j].departId);
//						console.log(depts[j].departName);
						if(depts[j].users.length>0){
							var users=depts[j].users;
							for(var k=0;k<users.length;k++){
								trees+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<li value='"+users[k].userId+"' class='us' onClick='getMore(this)'>"+users[k].userName+"</li>";
							}
							trees+="</ul>";
						}
						trees+="</li>";
					}
					trees+="</ul>";
				}
				trees+="</li>";
			}
			trees+="</ul>";
//			console.log(trees);
			$(".myPrin").eq(0).empty().append(trees);
		},
		error:function(res){
			console.log(res);
		}
	})
}

//点击负责人，自动填充信息
function getMore(obj){
	var id=$(obj).attr("value");
	//查询
	var data={
			"userId":id
	}
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/departuser',
		data:{
			"typeName":"DepartUser",
			"method":"findById",
			"data":JSON.stringify(data)
		},
		success:function(data){
//			console.log(data);
			var user=JSON.parse(data);
			console.log(user.userName);
			var departUserInfos=$(".departUser");
			departUserInfos.eq(0).val(user.userName);
			departUserInfos.eq(1).val(user.connectTelNo);
			departUserInfos.eq(2).val(user.connectMobileTelNo);
			departUserInfos.eq(3).val(user.faxes);			
		},
		error:function(res){
			console.log(res);
		}
	})
}

//上传图片
function uploadPic(){
	var photo = $('#myPhoto')[0].files[0];
    var formData = new FormData();
    formData.append("photo",photo);

    $.ajax({
        url:'http://localhost:8088/myOA/upload',
        dataType:'json',
        type:'POST',
//        async: false,
        data: formData,
        processData : false, // 使数据不做处理
        contentType : false, // 不要设置Content-Type请求头
        success: function(data){
        	console.log(data);
            if (data.status == 'ok') {
                alert('上传成功！');
            }
            var src=$("#myPic img")[0].src;
            $("#myPic img").attr('src','').attr('src','http://localhost:8088/myOA/uploads/photo.gif');
            $("#myPic").css('display','block');
        },
        error:function(response){
            console.log(response);
        }
    });
}

//获取指定部门的日程
$("#getDeptsPlan").click(function(){
	//获取机构id
	var branchId=$("#orgs").val();
	//获取部门id
	var deptId=$("#depts").val();
	//获取用户名
	var userName=$("#uName").val();
	//获取时间
	var sDate=$("#searchDate").val();
	//查询
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/schedules',
		data:{
			"method":"searchDeptCalendar",
			"branchId":branchId,
			"deptId":deptId,
			"userName":userName,
			"sDate":sDate
		},
		success:function(data){
			var j=JSON.parse(data);
			console.log(j);
			var cals="";
			for(var i=0;i<j.length;i++){
//				console.log(j[i]);
				cals+="<tr>";
				cals+="<td>"+j[i].scheduleId+"</td>";
				for(var k=0;k<7;k++){
					var myDate=new Date(j[i].beginTime);
//					myDate.setDate(17);
					if(myDate.getDay()==k){
						cals+="<td><i class='fa fa-tasks fa-lg'></i> "+j[i].title+"--"+j[i].beginTime+"</td>";
					}else{
						cals+="<td></td>";
					}
					
				}							
				cals+="</tr>";
			}
			$("#division tbody").empty().append(cals);
		},
		error:function(res){
			console.log(res);
		}
	});
});

//加载菜单排序
$("#showMenuOrders").click(function(){
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/sysfun',
		data:{
			"typeName":"SysFun",
			"method":"getOrders",
			"menuId":0
		},
		success:function(data){
			var menus=JSON.parse(data);
//			console.log(menus);
			
			var ms="";
			for(var i=0;i<menus.length;i++){
				ms+="<div class='mo' value='"+menus[i].nodeId+"' onclick='getSubMenuOrder(this)'><i class='fa fa-folder fa-lg'></i>"+menus[i].displayName+"</div>";
			}
			console.log(ms);
			$(".menuOrders").eq(0).empty().append(ms);
		},
		error:function(res){
			console.log(res);
		}
	});	
})

//加载子菜单顺序
var fMenuTitle="";
function getSubMenuOrder(ms){
	var fMenu=$(ms);
//	console.log(ms);
	var nodeId=fMenu.attr('value');
//	console.log(fMenu);
//	$(ms).text().substring(0,4);
//	console.log($(ms).text().substring(0,4));
	if(fMenu.children().length>2){
		return;
	}
	if(fMenuTitle==""||fMenuTitle!=$(ms).text().substring(0,4)){
		fMenuTitle=$(ms).text();
	}
	
//	console.log(nodeId);
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/sysfun',
		data:{
			"typeName":"SysFun",
			"method":"getOrders",
			"menuId":nodeId
		},
		success:function(data){
			var menus=JSON.parse(data);
			console.log(menus);
			var mss="";
			for(var i=0;i<menus.length;i++){
				mss+="<div class='smo' value='"+menus[i].nodeId+"'><i class='fa fa-folder fa-lg'></i>"+menus[i].displayName+"</div>";
			}
			fMenu.empty();
			fMenu.append("<i class='fa fa-folder fa-lg'></i>"+fMenuTitle);
			fMenu.append(mss);
		},
		error:function(res){
			console.log(res);
		}
	});
}












